package com.rui.study.algorithm.L_亿级流量网站架构_限流详解;

import com.google.common.util.concurrent.RateLimiter;

import java.util.concurrent.TimeUnit;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2019-03-06 11:20
 **/
public class SmoothWarmingUp {

    RateLimiter rateLimiter = RateLimiter.create(5,1000,TimeUnit.MILLISECONDS);

    private void process() {
        System.out.println(Thread.currentThread().getName() + " do something! [" + rateLimiter.acquire() + "]");
    }

    public static void main(String[] args) throws InterruptedException {
        SmoothWarmingUp smoothWarmingUp = new SmoothWarmingUp();
        for (int i = 0; i < 20; i++) {
            if (i == 5) {
                Thread.sleep(3000);
            }
            new Thread(()->smoothWarmingUp.process(), "My-Worker-" + i).start();
        }
    }
}
